package com.cgnpc.scp.common.utils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.Map;

/******************************
 * 用途说明: 消息模板渲染工具
 * 作者姓名: chenshijie
 * 创建时间: 2020/08/13 23:04
 ******************************/
public class FreemarkerUtil {

    private static final Logger LOGGER = LoggerFactory.getLogger(FreemarkerUtil.class);

    private static final String T_NAME = "template";

    public static String render(Map<String, String> map, String templateString) {
        try{
            StringWriter result = new StringWriter();
            Template t = new Template(T_NAME, new StringReader(templateString), new Configuration(Configuration.VERSION_2_3_23));
            t.process(map, result);
            return result.toString();
        }catch (Exception e) {
            LOGGER.error("模板渲染失败：{}",templateString,e);
        }

        return templateString;
    }

}
